package com.hula.onion.book.server.spider.platforms.lianzidai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/3/15.
 */
@Component
public class LianZiDaiSpiderHelper {
    public Map<String, Object> installRequestParams(LoginForm loginForm) {
        Map<String, Object> map = new HashMap<>(8);
        map.put("userName", loginForm.getUserName());
        map.put("password", loginForm.getPassword());
        map.put("pwd", loginForm.getPassword());
        map.put("encrypt", "0");
        map.put("openId", "");
        map.put("openType", "");
        map.put("count", "");
        map.put("redirectURL", "");
        return map;
    }


    /**
     * 解析账户余额
     *
     * @param accountHtml
     * @param accountHtml
     * @return
     */
    public CommonResponse parseAccountBalance(String accountHtml) {
        try {
            Document document = Jsoup.parse(accountHtml);
            String amount = document.selectFirst("li.count").children()
                    .get(1).children().get(0).text();
            String waitInterest = document.selectFirst("div.todaySy.memberSy").
                    children().get(1).children().get(0).text();
            String totalInterest = document.selectFirst("div.allSy.memberSy")
                    .children().get(1).children().get(0).text();
            AccountInfo accountInfo = new AccountInfo();
            accountInfo.setAmount(new BigDecimal(amount));
            accountInfo.setWaitIntrest(new BigDecimal(waitInterest));
            accountInfo.setTotalIntrest(new BigDecimal(totalInterest));
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }
}
